/*  This file is a part of JJSched source code
    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
    Copyright (C) 2008 Cestmir "CZestmyr" Houska

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/> */

package czestmyr.jjsched;

/**
 * Executes the actions that are parsed by CommandParser.
 */
public class ActionExecutor {
	ActionExecutor(Connector c, DatabaseConnector dbc, Config conf, TimeHandler t, jjsched j) {
		theConnector = c;
		theDBConnector = dbc;
		theConfig = conf;
		theMaster = j;
		theTime = t;
	}

	/**
	 * Executes the given action.
	 *
	 * @param action	The action to execute.
	 */
	public void execute(Action action) {
		String message = null;

		//DEBUG:
		action.dump();

		try {
			switch(action.id){
				case NONE:
					//Do nothing
				break;
				case ECHO:
					theConnector.sendMessage(action.sParam1, action.user);
				break;
				case ERROR:
					if (theConfig.getInt("ShowErrorMessages") != null &&
   	          			theConfig.getInt("ShowErrorMessages").intValue() == 1) {
						theConnector.sendMessage(action.sParam1, action.user);
					}	
				break;
				case MESSAGE:
					if (theConfig.getInt("ShowInfoMessages") != null &&
    	         		theConfig.getInt("ShowInfoMessages").intValue() == 1) {
						theConnector.sendMessage(action.sParam1, action.user);
					}	
				break;
				case REGISTER:	
   	                theDBConnector.registerUser(action.otherUser);
   	                message = "Registered user " + action.otherUser;
   	                //Inform the other user about registering
	                theConnector.sendMessage("You have been registered to JJSched. To read a short tutorial to help you using\n"
    	                                   + "JJSched, send message 'help tutorial' to this jabber contact.", action.otherUser);
				break;
                case UNREGISTER:
                    theDBConnector.unregisterUser(action.otherUser);
                    message = "Unregistered user " + action.otherUser;
   	                //Inform the other user about unregistering
	                theConnector.sendMessage("You have been unregistered from JJSched.", action.otherUser);
				break;
                case LIST_USERS:
                    message = theDBConnector.getUsers();
                break;
                case LIST_ADMINS:
                    message = theDBConnector.getAdmins();
                break;
                case LIST_GROUPS:
                    message = theDBConnector.getGroups();
                break;
				case QUIT:
					theMaster.quit();
				break;
				case PLAN:
                    theDBConnector.addEvent(action.plan);
                    message = "Added a plan for ";
                    if (action.plan.groupId == 0) {
                        message += action.plan.username;
                    } else {
                        message += action.plan.groupId;
                    }
                    message += " at "
                            + theTime.getFormattedTime(theDBConnector.getUserTimeOffset(action.plan.username)+action.plan.timestamp)
                            + " in user time.";
                break;
				case SETOFFSET:
                    theDBConnector.setUserTimeOffset(action.otherUser, action.lParam);
                    message = "The offset for user "+action.otherUser+" was set to "+theTime.getFormattedOffset(action.lParam);
				break;
                case GROUP_ADD:
                    message = theDBConnector.addGroup(action.sParam1, action.sParam2);
                break;
                case GROUP_MODIFY:
                    message = theDBConnector.modifyGroup(action.lParam, action.sParam1, action.sParam2, action.iParam);
                break;
                case GROUP_LISTUSERS:
                    message = theDBConnector.listGroupUsers(action.lParam);
                break;
                case GROUP_SUBSCRIBE:
                    message = theDBConnector.subscribeToGroup(action.lParam, action.otherUser);
                break;
                case GROUP_UNSUBSCRIBE:
                    message = theDBConnector.unsubscribeFromGroup(action.lParam, action.otherUser);
                break;
                case GROUP_PRIVILEGES:
                    message = theDBConnector.setUserGroupPrivileges(action.lParam, action.otherUser, action.iParam);
                break;
                case GROUP_ADMIN:
                    message = theDBConnector.setAdminPrivileges(action.otherUser, action.iParam);
                break;
                case GROUP_REMOVE:
                    message = theDBConnector.removeGroup(action.lParam);
                break;
				case GROUP_LISTEVENTS:
					message = theDBConnector.listGroupEvents(action.lParam);
				break;
				case LIST_EVENTS:
					message = theDBConnector.listUserEvents(action.user);
				break;
			}
		} catch (Exception e) {
            if (theConfig.getInt("ShowErrorMessages") != null &&
                theConfig.getInt("ShowErrorMessages").intValue() == 1)
                theConnector.sendMessage("Operation unsuccessful: "+e.getMessage(), action.unescapedUser);
		}

		// If there's a status message to be sent back to the user, send it
		if (message != null &&
            theConfig.getInt("ShowInfoMessages") != null &&
            theConfig.getInt("ShowInfoMessages").intValue() == 1){
			theConnector.sendMessage(message, action.unescapedUser);
		}

		return;
	}

    private Connector theConnector;
	private Config theConfig;
    private DatabaseConnector theDBConnector;
	private TimeHandler theTime;
	private jjsched theMaster;
}

